package com.wtwd.campus.receiver;

import com.wtwd.campus.entity.MqMessage;
import com.wtwd.campus.utils.InjectRedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;

public interface IReceiverHandler {

    Logger logger = LoggerFactory.getLogger(IReceiverHandler.class);

    void handlerMessage(MqMessage mqMessage);


    /**
     * 根据设备回复中的年月日和时分秒获取数据定位时间
     * @param date 年月日
     * @param time 时分秒
     * @return
     */
    default Long getTime(String date, String time) {
        String newDate = getNewTimeParame(date);
        String newTime = getNewTimeParame(time);
        int year =Integer.parseInt("20" + newDate.substring(4));
        int month = Integer.parseInt(newDate.substring(2, 4));
        int day =Integer.parseInt( newDate.substring(0,2));
        int hour =Integer.parseInt( newTime.substring(0, 2));
        int minute =Integer.parseInt( newTime.substring(2, 4));
        int second =Integer.parseInt( newTime.substring(4));
        Calendar calendar=Calendar.getInstance();
        calendar.set(year,month-1,day,hour,minute,second);
        return calendar.getTimeInMillis();
    }

    /**
     * 存储为入库的设备
     * @param imei
     */
    default void saveUnInStorage(String imei){
        //数据库中存入未入库的imei
        InjectRedisService.getRedisService().setUnInStorageDevice(imei);
        //返回给netty断开通道
    }

    /**
     * 将'-'替换成'' 如果不足六位用0补足 多的去除
     * @param time
     * @return
     */
    static String getNewTimeParame(String time){
        StringBuffer stringBuffer = new StringBuffer();

        if(time.contains("-")){
            stringBuffer.append(time.replaceAll("-",""));
        }else{
            stringBuffer.append(time);
        }

        while (stringBuffer.length() < 6){
            stringBuffer.append("0");
        }

        return stringBuffer.toString().substring(0,6);
    }

    public static void main(String[] args) {
        String newTimeParame = getNewTimeParame("21191");
        System.out.println(newTimeParame);
//        System.out.println("216191".replaceAll("-",""));
//        Long time = getTime1("21191", "-21-6-19");
//        System.out.println(time);
    }

    static Long getTime1(String date, String time) {
        String newDate = "";
        String newTime = "";
        if(date.contains("-")){
            newDate = date.replaceAll("-","");
        }else{
            newDate = date;
        }
        if(time.contains("-")){
            newTime = time.replaceAll("-","");
        }else{
            newTime = time;
        }
        int year =Integer.parseInt("20" + newDate.substring(4));
        int month = Integer.parseInt(newDate.substring(2, 4));
        int day =Integer.parseInt(newDate.substring(0,2));
        int hour =Integer.parseInt(newTime.substring(0, 2));
        int minute =Integer.parseInt(newTime.substring(2, 4));
        int second =Integer.parseInt(newTime.substring(4));
        Calendar calendar=Calendar.getInstance();
        calendar.set(year,month-1,day,hour,minute,second);
        return calendar.getTimeInMillis();
    }

}
